package com.uzhie.jt808.task;

import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.JT808SysCache;
import com.uzhie.jt808.jt1078.entity.JT_1205;
import com.uzhie.jt808.msg.JT808MsgBean;
import com.uzhie.web.service.listener.MQSendService;
import io.netty.buffer.ByteBufUtil;
import org.apache.log4j.Logger;

/**
 * Created by TangFei on 2018/9/1.
 * 1205 终端上传音视频资源列表
 */
public class Task1205 implements Runnable {

    private Logger logger = Logger.getLogger(getClass());

    private JT808MsgBean msgBean;
    private MQSendService mqSendService;
    private JT808SysCache sysCache;
    private int flowNum;

    public Task1205(JT808MsgBean msgBean, MQSendService mqSendService, JT808SysCache sysCache) {
        this.msgBean = msgBean;
        this.mqSendService = mqSendService;
        this.sysCache = sysCache;
        this.flowNum = JT_1205.getNumber(msgBean.getBodyBytes());
    }

    @Override
    public void run() {
        if (msgBean.getHeader().isHasSubPakage()) {
            // 有分包
            logger.info("SIM：" + msgBean.getHeader().getSimNum() + " 总包数：" + msgBean.getHeader().getTotalSubPackage() + " 包序号：" + msgBean.getHeader().getNumSubPackage() + " 消息体：" + ByteBufUtil.hexDump(msgBean.getBodyBytes()));
        } else {
            // 无分包
            mqSendService.sendMessage(JT808Constant.MSG_ID.PLA_REQ_PLAYBACK_CATALOG, sysCache.getCacheVal(flowNum).toString(), msgBean.getBodyBytes());
            sysCache.remove(flowNum);
        }
    }
}
